﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Battleship_569
{
    class TCP
    {
        //create server
        public static bool Server()
        {
            try
            {
                IPAddress add = IPAddress.Any;
                Global.SocketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint ipEndPoint = new IPEndPoint(add, 8080);

                Global.SocketServer.Bind(ipEndPoint);

                Global.SocketServer.Listen(1);

                Global.SocketClientofServer = Global.SocketServer.Accept();

                //string senddata = "";
                //byte[] data = new byte[1024];
                //data = Encoding.ASCII.GetBytes(senddata);

                //socketClient.Send(data, data.Length, SocketFlags.None);

                //nhan du lieu
             //   byte[] data = new byte[Global.MEMORY_BUFFER];

                //kiem tra rong
              //  int length = Global.SocketClientofServer.Receive(data);


              //  string senddata = Encoding.ASCII.GetString(data, 0, length);
                string data = ReceiveData();
                if (data.Equals("Connected"))
                {
                    return true;
                }
            }catch(SocketException ex)
            {
                return false;
            }
            return false;
        }

        //join server
        public static bool Client()
        {
            try
            {
                // ASCIIEncoding encoding = new ASCIIEncoding();
                //TcpClient client = new TcpClient();

                //client.Connect(Global.HostIP, 8080);
                //Stream stream = client.GetStream();

                Global.SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(Global.HostIP), 8080);
                Global.SocketClient.Connect(ipEndPoint);

                // // nhan du lieu 
                // byte[] dataArr = new byte[1024];
                //// int length = socketClient.Receive(dataArr);

                // // convert ve string 
                string s = "Connected";
                SendData(s);
                //byte[] data = Encoding.ASCII.GetBytes(s);

                ////dataArr = Encoding.ASCII.GetBytes(data);
                //Global.SocketClient.Send(data, data.Length, SocketFlags.None);
                return true;
            }
            catch (SocketException ex)
            {
                return false;
            }
            catch(Exception )
            {
                return false;
            }


        }

        public static void SendData(string stringdata)
        {
            //send data for client
            if(!Global.isCreateGame)
            {
                byte[] dataArray = new byte[Global.MEMORY_BUFFER];
                dataArray = Encoding.ASCII.GetBytes(stringdata);

                Global.SocketClient.Send(dataArray, dataArray.Length, SocketFlags.None); 
            }
                //send data for server
            else
            {
                byte[] dataArray = new byte[Global.MEMORY_BUFFER];
                dataArray = Encoding.ASCII.GetBytes(stringdata);
                Global.SocketClientofServer.Send(dataArray, dataArray.Length, SocketFlags.None);
            }
           
            
        }

        public static string ReceiveData()
        {
            //for client
            if(!Global.isCreateGame)
            {
                byte[] dataArray = new byte[Global.MEMORY_BUFFER];
                var length = Global.SocketClient.Receive(dataArray);
                string stringdata = Encoding.ASCII.GetString(dataArray, 0, length);
                return stringdata;
            }
                //for server
            else
            {
                byte[] dataArray = new byte[Global.MEMORY_BUFFER];
                var length = Global.SocketClientofServer.Receive(dataArray);
                string stringdata = Encoding.ASCII.GetString(dataArray, 0, length);
                return stringdata;
            }

            //Console.Write("Client: {0}", stringdata);
            //if (stringdata.ToUpper().Equals("EXIT"))
            //{
            //    break;
            //}

            //gui data cho client
            //Console.WriteLine("Server: ");
            //stringdata = Console.ReadLine();
            //dataArray = new byte[1024];

            //dataArray = Encoding.ASCII.GetBytes(stringdata);
            //Global.SocketClient.Send(dataArray, dataArray.Length, SocketFlags.None);

        }
    }
}
